/*
 * Copyright (C) 2017 WordPlat Open Source Project
 *
 *      https://wordplat.com/InteractiveKLineView/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.ikvstockchart.drawing;


import com.wordplat.ikvstockchart.align.YLabelAlign;
import com.wordplat.ikvstockchart.entry.SizeColor;
import com.wordplat.ikvstockchart.render.AbstractRender;
import com.wordplat.ikvstockchart.utils.StringUtils;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.agp.utils.TextAlignment;

import java.text.DecimalFormat;

/**
 * <p>StockIndexYLabelDrawing</p>
 * <p>Date: 2017/3/28</p>
 *
 * @author afon
 * @since 2021-05-09
 */
public class StockIndexYLabelDrawing implements IDrawing {
    /**
     * Y 轴标签的画笔
     */
    private Paint yLabelPaint;
    /**
     * 用于 labelPaint 计算文字位置
     */
    private final Paint.FontMetrics fontMetrics = new Paint.FontMetrics(0, 0, 0, 0, 0);
    private final DecimalFormat decimalFormatter = new DecimalFormat("0.00");

    private final RectFloat indexRect = new RectFloat();
    /**
     * Y 轴标签对齐方向
     */
    private YLabelAlign yLabelAlign;

    @Override
    public void onInit(RectFloat contentRect, AbstractRender render) {
        final SizeColor sizeColor = render.getSizeColor();

        if (yLabelPaint == null) {
            yLabelPaint = new Paint();
            yLabelPaint.setAntiAlias(true);
            yLabelPaint.setTextSize(StringUtils.floatToInt(sizeColor.getYLabelSize()));
        }
        yLabelPaint.setColor(new Color(sizeColor.getYLabelColor()));
        yLabelPaint.getFontMetrics();
        yLabelAlign = sizeColor.getYLabelAlign();
        if (yLabelAlign == YLabelAlign.RIGHT) {
            yLabelPaint.setTextAlign(TextAlignment.RIGHT);
        }

        indexRect.left = contentRect.left;
        indexRect.top = contentRect.top;
        indexRect.bottom = contentRect.bottom;
        indexRect.right = contentRect.right;
    }

    @Override
    public void computePoint(int minIndex, int maxIndex, int currentIndex) {

    }

    @Override
    public void onComputeOver(Canvas canvas, int minIndex, int maxIndex, float minY, float maxY) {
        float labelX = yLabelAlign == YLabelAlign.LEFT ? indexRect.left + 5 : indexRect.right - 5;

        canvas.drawText(yLabelPaint, decimalFormatter.format(maxY), labelX, indexRect.top - fontMetrics.top);
        canvas.drawText(yLabelPaint, decimalFormatter.format(minY), labelX, indexRect.bottom - fontMetrics.bottom);
    }

    @Override
    public void onDrawOver(Canvas canvas) {

    }
}
